package com.jiaozi.core.security.pojo;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.jiaozi.base.enums.sys.StateEnum;
import com.jiaozi.core.security.pojo.login.LuDepartmentVO;
import com.jiaozi.core.security.pojo.login.LuMenuVO;
import com.jiaozi.core.security.pojo.login.LuRoleVO;
import com.jiaozi.core.security.pojo.login.LuUserVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 用户登录权限
 */
@Schema(name = "用户登录信息",description = "用户登录信息")
@Data
public class LoginUserDetails implements UserDetails {
    @Schema(description = "用户信息")
    private LuUserVO loginUser;
    //所属部门
    @Schema(description = "所属部门")
    private LuDepartmentVO department;
    @Schema(description = "所属角色")
    private List<LuRoleVO> roles;

    @Schema(description = "所属菜单")
    private List<LuMenuVO> menus;

    @Schema(description = "所属权限")
    private List<String> permissions;

    public LoginUserDetails(LuUserVO loginUser) {
        this.loginUser = loginUser;
    }


    @Override
    @JSONField(serialize = false)
    @Schema(hidden = true)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of();
    }

    @Override
    @JSONField(serialize = false)
    @Schema(hidden = true)
    public String getPassword() {
        return this.loginUser.getPassword();
    }

    @Override
    @JSONField(serialize = false)
    @Schema(hidden = true)
    public String getUsername() {
        return this.loginUser.getUserName();
    }

    /**
     * 账户是否未到期
     * @return
     */
    @Override
    @JSONField(serialize = false)
    @Schema(hidden = true)
    public boolean isAccountNonExpired() {
        if(DateUtil.compare(loginUser.getExpiredDate(),new Date())>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     *账户是否锁定
     * @return
     */
    @Override
    @JSONField(serialize = false)
    @Schema(hidden = true)
    public boolean isAccountNonLocked() {
//      默认即可，已在LoginLockServiceImpl中处理相关逻辑
        return true;
    }

    /**
     * 凭证是否未到期
     * @return
     */
    @Override
    @JSONField(serialize = false)
    @Schema(hidden = true)
    public boolean isCredentialsNonExpired() {
        //默认即可，已在JWT中处理相关逻辑
        return true;
    }

    /**
     * 是否可以使用
     * @return
     */
    @Override
    @JSONField(serialize = false)
    @Schema(hidden = true)
    public boolean isEnabled() {
        if(this.loginUser.getIsEnabled()== StateEnum.NORMAL){
            return true;
        }else{
            return false;
        }
    }




}
